#include "tablemodel.h"

TableModel::TableModel(
    QObject *parent)
    : QAbstractTableModel{parent}
    , m_datas{new Table}
{

}

TableModel::~TableModel()
{
    delete m_datas;
}

int TableModel::rowCount(const QModelIndex &parent) const
{
    return m_datas->data().size();
}

int TableModel::columnCount(const QModelIndex &parent) const
{
    return Column_NUM;
}

QVariant TableModel::data(const QModelIndex &index, int role) const
{
    if(!index.isValid())
        return QVariant();

    auto datas = m_datas->data();
    int row = index.row();
    int col = index.column();
    if(row >= 0 && row < datas.size()) {
        DataItem *item = datas.at(row);
        if(item) {
            switch (col) {
            case Column_SN:
                if(role == Qt::DisplayRole || role == Qt::EditRole)
                    return item->sn;
            case Column_Name:
                if(role == Qt::DisplayRole || role == Qt::EditRole)
                    return item->name;
            case Column_Hotspot:
                if(role == Qt::DisplayRole || role == Qt::EditRole)
                    if(role == Qt::DisplayRole)
                        return getSupportedString(item->hotspot);
                    else
                        return item->hotspot;
            case Column_Usb:
                if(role == Qt::DisplayRole || role == Qt::EditRole)
                    if(role == Qt::DisplayRole)
                        return getUsbTypeString((UsbType)item->usb);
                    else
                        return item->usb;
            case Column_Note:
                if(role == Qt::DisplayRole || role == Qt::EditRole)
                    return item->note;
            default:
                break;
            }
        }
    }
    return QVariant();
}

bool TableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if(!index.isValid())
        return false;
    auto datas = m_datas->data();
    int row = index.row();
    int col = index.column();
    if(row >= 0 && row < datas.size()) {
        DataItem *item = datas.at(row);
        if(item) {
            switch (col) {
            case Column_SN: {
                QString v = value.toString();
                if(!v.isEmpty()) {
                    item->sn = v;
                    return true;
                }
                break;
            }
            case Column_Name: {
                QString v = value.toString();
                if(!v.isEmpty()) {
                    item->name = v;
                    return true;
                }
                break;
            }
            case Column_Hotspot: {
                if(role == Qt::EditRole) {
                    item->hotspot = value.toInt();
                    return true;
                }
                break;
            }
            case Column_Usb: {
                if(role == Qt::EditRole) {
                    item->usb = value.toInt();
                    return true;
                }
                break;
            }
            case Column_Note:
                item->note = value.toString();
                return true;
            default:
                break;
            }
        }
    }

    return false;
}

QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role != Qt::DisplayRole)
        return QVariant();
    if (orientation == Qt::Vertical)
        return section+1;

    switch (section) {
    case Column_SN:
        return tr("SN");
    case Column_Name:
        return tr("Name");
    case Column_Hotspot:
        return tr("Hotspot");
    case Column_Usb:
        return tr("Usb");
    case Column_Note:
        return tr("Note");
    default:
        break;
    }
    return QVariant();
}

Qt::ItemFlags TableModel::flags(const QModelIndex &index) const
{
    if(!index.isValid())
        return Qt::NoItemFlags;
    if(index.column() == Column_SN)
        return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
    return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled;
}

bool TableModel::load(const QString &fileName, Table::SaveFormat format, bool clearOldData)
{
    beginResetModel();
    bool ret = m_datas->load(fileName, format, clearOldData);
    endResetModel();
    return ret;
}

bool TableModel::save(const QString &fileName, Table::SaveFormat format)
{
    return m_datas->save(fileName, format);
}

QString getUsbTypeString(UsbType type)
{
    switch (type) {
    case USB_200:
        return "USB2.0";
    case USB_300:
        return "USB3.0";
    case USB_310:
        return "USB3.1";
    case USB_320:
        return "USB3.2";
    default:
        return "Unsupported";
    }
}

QString getSupportedString(bool supported)
{
    return supported ? "Supported" : "Unsupported";
}
